package com.xquant.trade.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.Optional;
import java.util.regex.Pattern;

/**
 * @Author: xpp -_- -_-
 * @Date: 2021/6/18 14:12
 */
public class PasswordCheckUtil {

//    private final static String passwordComplexity = ParamCache.getString("passwordComplexity");
    private final static String passwordComplexity = "";

    //密码至少长度8-20
    public static final String REGEX_PASSWORD_LOW = "^[a-zA-Z0-9-*/+.~!@#$%^&*()]{8,20}$";

    //密码至少包含数字跟字母，长度8-20
    public static final String REGEX_PASSWORD_Middle = "^(?=.*([a-zA-Z].*))(?=.*[0-9].*)[a-zA-Z0-9-*/+.~!@#$%^&*()]{8,20}$";

     // 至少有1个大写字符1个小写字符1个特殊字符长度至少1个数字8-20
    public static final String REGEX_PASSWORD_High = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&.])[A-Za-z\\d$@$!%*?&.]{8,20}$";

    public static void main(String[] args) {
//        System.out.println("校验结果 = " + check_low("123456789"));
//        System.out.println("校验结果 = " + check_middle("1234567a"));
//        System.out.println("校验结果 = " + check_high("1xxxxxxaA*"));
    }

    /**
     * 校验密码
     *
     * @param password
     * @return 校验通过返回true，否则返回false
     */

    public static boolean check(String password) {
        if (!Optional.ofNullable(passwordComplexity).isPresent() ||StringUtils.isEmpty(passwordComplexity)) {
            return check_low(password);
        }
        return Pattern.matches(passwordComplexity, password);
    }

    public static boolean check_low(String password) {
        return Pattern.matches(REGEX_PASSWORD_LOW, password);
    }

    public static boolean check_middle(String password) {
        return Pattern.matches(REGEX_PASSWORD_Middle, password);
    }

    public static boolean check_high(String password) {
        return Pattern.matches(REGEX_PASSWORD_High, password);
    }

}
